<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>FSDKP - 安装</title>
<script language="JavaScript" src="inc/jquery-1.3.2.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Generator" content="EditPlus">
<meta name="Author" content="SULei">
<meta name="Keywords" content="魔兽世界DKP系统 DKP程序 公会DKP">
<meta name="Description" content="">
<link href="<?php echo $StylePath;?>/main.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="style/styleplus.js"></script>
<script language="JavaScript" src="inc/UBB_UI.js"></script>
<style type="text/css" media="all">

</style>
</head>

<body>

<!-- 数据库信息表单 -->

<!-- 建库建表 -->
<?php
	if (isset($_POST["mysql_url"])) {
		echo "<p>链接到Mysql数据库...</p>";
		//链接MYSQL
		if ($db_link = mysql_connect($_POST["mysql_url"],$_POST["user_name"],$_POST["user_pwd"])) {
			echo '<p>链接成功</p>';
			mysql_query("set names 'utf8';");
			//检查是否有同名库
			if (!mysql_select_db ($_POST["db_name"])) {
				//建库
				createdatabase($_POST["db_name"]);
				//建全部表
				createtables("all");
				//灌入初始数据
				importdata($_POST["db_name"]);
				//生成配置文件
				if (createcfgfile($_POST["mysql_url"],$_POST["user_name"],$_POST["user_pwd"],$_POST["db_name"])) {
					echo "FSDKP 已经安装完成,<strong>请删除FSDKP目录中的install文件夹</strong>,点击打开<a href='../default.php'>系统首页</a>";
				} else {
					echo "安装失败.";
				}
			} else {
				//有同名库,确定是否要覆盖
				if ($_POST["replace"]) {
					//删现有库
					mysql_query("DROP DATABASE ".$_POST["db_name"]);
					//建库
					createdatabase($_POST["db_name"]);
					//建全部表
					createtables("all");
					//灌入初始数据
					importdata($_POST["db_name"]);
					//生成配置文件
					if (createcfgfile($_POST["mysql_url"],$_POST["user_name"],$_POST["user_pwd"],$_POST["db_name"])) {
						echo "FSDKP 已经安装完成,<strong>请删除FSDKP目录中的install文件夹</strong>,点击打开<a href='../default.php'>系统首页</a>";
					} else {
						echo "安装失败.";
					}
				} else {
					//返回错误
					echo "<br>Error: 库 ".$_POST["db_name"]." 已存在<br>";
				}
			}
		//链接失败
		}else{
			echo "<p>安装失败<br>原因:访问数据库被拒绝,请检查参数并再次尝试</p>";
		}
	}
?>
<!-- 参数设置 -->
<!-- 完成 -->





<?php

//创建库
function createdatabase($str_db_name) {
	if (mysql_query("create database ".$str_db_name)) {
		print "数据库创建...成功";
		return true;
	} else {
		print "数据库创建...失败";
		return false;
	}
};


//创建FSDKP的所有表
function createtables($str_table_name) {
	$arr_froms["raid_event"] = "CREATE TABLE `fsdkp`.`raid_event` ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, event_raidid SMALLINT,event_class VARCHAR(50), event_object VARCHAR(255), event_object2 VARCHAR(255), event_dkp VARCHAR(10), event_time DATETIME, raid_class SMALLINT, event_dkp2 VARCHAR(50), event_object2_tooltip TEXT, PRIMARY KEY(`id`) ) ENGINE = InnoDB";

	$arr_froms["raid_class"] = "CREATE TABLE `fsdkp`.`raid_class` ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, class_name VARCHAR(255) NOT NULL, class_note VARCHAR(255), closing_date DATETIME, PRIMARY KEY(`id`) )ENGINE = InnoDB";

	//原结构$arr_froms["raid"] = "CREATE TABLE `fsdkp`.`raid` (id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, raid_name VARCHAR(255) NOT NULL, raid_concentratetime DATETIME, raid_actiontime DATETIME, raid_personnel TINYINT, raid_plan TEXT, raid_ruler VARCHAR(50), raid_signup TEXT, raid_player TEXT, raid_latecomer TEXT, raid_dropout TEXT, raid_misconduct TEXT, raid_extra TEXT, raid_commander VARCHAR(200), raid_mtank VARCHAR(200), raid_mheal VARCHAR(200), raid_endtime DATETIME, raid_drop TEXT, raid_allot TEXT, raid_stat_dp TEXT, raid_stat_hp TEXT, raid_report TEXT, raid_step TINYINT, raid_class SMALLINT,   PRIMARY KEY(`id`) )ENGINE = InnoDB";

	$arr_froms["raid"] = "CREATE TABLE `fsdkp`.`raid` (id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, raid_name VARCHAR(255) NOT NULL, raid_concentratetime DATETIME, raid_actiontime DATETIME, raid_personnel TINYINT, raid_plan TEXT, raid_player TEXT, raid_commander VARCHAR(200), raid_endtime DATETIME, raid_step TINYINT, raid_class SMALLINT,   PRIMARY KEY(`id`) )ENGINE = InnoDB";

	$arr_froms["player_dkp"] = "CREATE TABLE `fsdkp`.`player_dkp` (`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, player_id SMALLINT NOT NULL, player_name VARCHAR(20) NOT NULL, raid_class TINYINT , player_dkp SMALLINT , player_Basedkp SMALLINT ,   PRIMARY KEY(`id`) )ENGINE = InnoDB";

	//原结构$arr_froms["player"] = "CREATE TABLE `fsdkp`.`player` (`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,player_name VARCHAR(20) NOT NULL, player_guildlevel VARCHAR(20) , player_level TINYINT , player_tribe VARCHAR(50) , player_profession VARCHAR(50) , player_honor VARCHAR(50) , player_jointime DATETIME , player_BaseDKP SMALLINT , player_DKP SMALLINT, player_activist TINYINT , player_skill_1 VARCHAR(50) , player_skill_2 VARCHAR(50), player_note TEXT , player_looted TEXT , player_raidnum SMALLINT , player_state VARCHAR(50) , player_lastraid DATETIME , player_log TEXT,   PRIMARY KEY(`id`))ENGINE = InnoDB";

	$arr_froms["player"] = "CREATE TABLE `fsdkp`.`player` (`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,player_name VARCHAR(20) NOT NULL, player_level TINYINT , player_race VARCHAR(50) , player_class VARCHAR(50) , player_jointime DATETIME , player_activist TINYINT , player_note TEXT , player_looted TEXT , player_raidnum SMALLINT , player_state VARCHAR(50) , player_lastraid DATETIME , player_log TEXT,   PRIMARY KEY(`id`))ENGINE = InnoDB";

	//弃用$arr_froms["lib_event"] = "CREATE TABLE `fsdkp`.`lib_event` (  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,   `event_name` VARCHAR(50) ,   `event_class` VARCHAR(50) ,   `event_note` VARCHAR(50),   `event_essence` VARCHAR(50),   PRIMARY KEY(`id`) )ENGINE = InnoDB";

	$arr_froms["instance"] = "CREATE TABLE `fsdkp`.`instance` (  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,   `SN` SMALLINT ,   `Killed` tinyint(1) ,   PRIMARY KEY(`id`) )ENGINE = InnoDB";


	$arr_froms["gbook"] = "CREATE TABLE `fsdkp`.`gbook` (`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,   `gbook_raidid` VARCHAR(6) ,   `gbook_poster` VARCHAR(50) ,   `gbook_content` TEXT,   `gbook_time` DATETIME,   PRIMARY KEY(`id`) )ENGINE = InnoDB;";

	$arr_froms["guild"] = "CREATE TABLE `fsdkp`.`guild` (  `guild_name` VARCHAR(45) , `guild_sname` VARCHAR(70) , `guild_server` VARCHAR(30) , `guild_playernum` SMALLINT , `guild_master` VARCHAR(45) , `guild_supervisor` VARCHAR(45) , `guild_createtime` DATETIME,   `guild_raidnum` SMALLINT,  `guild_raidplan` VARCHAR(255) , `guild_rules` TEXT , `guile_DKPrules` TEXT , `guild_style` VARCHAR(30) , `guild_para` VARCHAR(255) , PRIMARY KEY(`guild_name`) )ENGINE = InnoDB;";

	$arr_froms["dkplist"] = "CREATE TABLE `fsdkp`.`dkplist` (  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,   `playerid` SMALLINT,   `dkp` FLOAT(6,2),   `raidid` SMALLINT,   PRIMARY KEY(`id`) )ENGINE = InnoDB;";

	//弃用$arr_froms["class_equip"] = "CREATE TABLE `fsdkp`.`class_equip` (  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,   `class_name` VARCHAR(25) NOT NULL,   PRIMARY KEY(`id`) )ENGINE = InnoDB";

	$arr_froms["article"] = "CREATE TABLE `fsdkp`.`article` (  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,   article_class VARCHAR(50),   article_title VARCHAR(200) NOT NULL, article_text TEXT, article_date DATETIME, article_editor VARCHAR(50),   PRIMARY KEY(`id`) )ENGINE = InnoDB;";

	$arr_froms["admin"] = "CREATE TABLE `fsdkp`.`admin` (  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,   `admin_name` VARCHAR(50) NOT NULL,   `admin_nickname` VARCHAR(50),   `admin_password` VARCHAR(50) NOT NULL,   `admin_level` VARCHAR(5),   PRIMARY KEY(`id`) )ENGINE = InnoDB;";

	echo "<p>开始建表</p>";
	echo "<ol>";
	foreach ( $arr_froms as $key => $value ) {
		if (mysql_query($value)) {
			echo "<li>".$key." 表创建成功</li>";
		} else {
			echo "<li>".$key." 表创建失败, 原因:".mysql_error()."</li>";
		}
	}
	echo "</ol>";
};


//灌入初始数据
function importdata($database) {
	mysql_select_db($database);
	$arr_data[0] = "INSERT article (article_class,article_title,article_text,article_date,article_editor) VALUES ('News','测试文章标题','测试文章测试文章内容','".date("Y-m-d H:i:s")."','SULei')";
	$arr_data[1] = "INSERT admin (admin_name,admin_nickname,admin_password,admin_level) VALUES ('admin','admin','d033e22ae348aeb5660fc2140aec35850c4da997','0')"; //d033e22ae348aeb5660fc2140aec35850c4da997 是admin的SHA1散列
	$arr_data[2] = "INSERT guild (guild_name,guild_sname,guild_server,guild_playernum,guild_createtime,guild_style,Guild_para) VALUES ('公会名','公会副标题','公会服务器名','0','".date("Y-m-d H:i:s")."','style','|http://cn.wowarmory.com/images/icons/43x43/|.png|0|0|5|0|0|0|')";
	$arr_data[3] = "INSERT raid_class (class_name,closing_date) VALUES ('A类副本','2009-1-1 12:00:00')";
	$arr_data[4] = "INSERT raid_class (class_name,closing_date) VALUES ('B类副本','2009-1-1 12:00:00')";

	echo "<p>灌入初始数据</p>";
	foreach ( $arr_data as $key => $value ) {
		if (!mysql_query($value)) {
			echo $value." 执行出错".mysql_error()."<br>"; 
		};
	}
}






//创建配置文件
function createcfgfile($server,$admin,$pwd,$database) {
	echo "创建配置文件...";
	$text .= "<?php\n";
	$text .= "\$server_url = '".$server."'; \n";
	$text .= "\$db_admin = '".$admin."'; \n";
	$text .= "\$db_pwd = '".$pwd."'; \n";
	$text .= "\$db_name = '".$database."'; \n";
	$text .= "?>\n";
	
	//open
	$cfgfile = fopen("../db_config.php","w");
	if(!fwrite($cfgfile,$text)) {
		echo "<p>文件写入错误.</p>";
		return false;
	} else {
		echo "<p>完成.</p>";
	};
	fclose($cfgfile);
	return true;
}


?>


<div class="install-box">
	<form method="post" action="?">
		<h4>安装 FSDKP</h4>
		<fieldset>
			数据库地址:<input type="text" name="mysql_url" value="localhost" /><br>
			管理账号:<input type="text" name="user_name" value="fsdkp" /><br>
			密码:<input type="text" name="user_pwd" value="1234" /><br>
			数据库命名:<input type="text" name="db_name" value="fsdkp" /> <input type="checkbox" name="replace" > 覆盖现有的同名数据库<br>
			<input type="submit" value="Submit">
		</fieldset>
	</form>
</div>


	</body>
</html>